package cn.edu.zjut.mina.bean;

import java.util.Random;
import java.util.Scanner;

/**
 * 
 * @author 张宇翔 2016/12/16
 * bet  下注
 * in   跟
 * out  不跟
 * fold 不玩了
 * 该版本没有加入房间创建的概念，等有数据库了再加
 * 中途退出也没有做
 */

public class PlayingCards {
	//常量区块
	public static final int MAX_NUMBER_OF_PEOPLE = 10; //最多参与人数
	/**
	 * 牌的花色   0 方块      1 草花      2  红桃    3黑桃
	 */
	private static final int DIAMOND = 0;//方块
	private static final int CLUB = 1;//草花
	private static final int HEART = 2;//红桃
	private static final int SPADE = 3;//黑桃
	private static final Card[] mCards = {new Card(DIAMOND, 1), new Card(DIAMOND, 2), new Card(DIAMOND, 3), new Card(DIAMOND, 4), new Card(DIAMOND, 5),
		                            new Card(DIAMOND, 6), new Card(DIAMOND, 7), new Card(DIAMOND, 8), new Card(DIAMOND, 9), new Card(DIAMOND, 10),
		                            new Card(DIAMOND, 11), new Card(DIAMOND, 12), new Card(DIAMOND, 13),
		                            new Card(CLUB, 1), new Card(CLUB, 2), new Card(CLUB, 3), new Card(CLUB, 4), new Card(CLUB, 5),
		                            new Card(CLUB, 6), new Card(CLUB, 7), new Card(CLUB, 8), new Card(CLUB, 9), new Card(CLUB, 10),
		                            new Card(CLUB, 11), new Card(CLUB, 12), new Card(CLUB, 13),
		                            new Card(HEART, 1), new Card(HEART, 2), new Card(HEART, 3), new Card(HEART, 4), new Card(HEART, 5),
		                            new Card(HEART, 6), new Card(HEART, 7), new Card(HEART, 8), new Card(HEART, 9), new Card(HEART, 10),
		                            new Card(HEART, 11), new Card(HEART, 12), new Card(HEART, 13),
		                            new Card(SPADE, 1), new Card(SPADE, 2), new Card(SPADE, 3), new Card(SPADE, 4), new Card(SPADE, 5),
		                            new Card(SPADE, 6), new Card(SPADE, 7), new Card(SPADE, 8), new Card(SPADE, 9), new Card(SPADE, 10),
		                            new Card(SPADE, 11), new Card(SPADE, 12), new Card(SPADE, 13)};
	/*private static String[] cards = {"黑桃A","黑桃2","黑桃3","黑桃4","黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K",
		                             "红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9","红桃10","红桃J","红桃Q","红桃K",
		                             "草花A","草花2","草花3","草花4","草花5","草花6","草花7","草花8","草花9","草花10","草花J","草花Q","草花K",
		                             "方块A","方块2","方块3","方块4","方块5","方块6","方块7","方块8","方块9","方块10","方块J","方块Q","方块K"};
	*/
	public static final int NUMBER_OF_CARDS = 5;//每人的牌数
	
	/**
	 * 
	 * 
	 * 
	 * 修改。。。。没有压注
	 */
	
	public static final int[] RATIO = {1,2,3};//压注倍率    (可选项                  )
	
	//成员变量区块
	private static Random random = new Random(System.currentTimeMillis());
	private static Player[] players;
	private static Boolean isFixed;//是否固定庄   true  固定庄       false   破牛庄
	private static int mCriticalOfTwoTimes;//2倍的临界值(也就是包不包含7)
	private static int mActualParticipation = 0;//实际参与人数(2-10)
	private static int mRound;//一次对局的局数    一张房卡对应的局数
	private static int bankerNumber = 0;//庄家编号(从0开始)
	
	
	//构造方法区块
	
	//方法区块
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		
		/**
		 * 初始化变量    根据用户选择而定
		 */
		isFixed = false;//破牛庄
		mCriticalOfTwoTimes = 8;//临界值为8      不包含7
		mRound = 4;//一次4局
		
		System.out.println("实际参与人数(范围为2-10)");
		mActualParticipation = scan.nextInt();    //一般定为4
		if (mActualParticipation < 2 || mActualParticipation > MAX_NUMBER_OF_PEOPLE) {
			System.out.println("实际参与人数范围为2-10");
			return;
		}
		
		//创建玩家
		players = new Player[mActualParticipation];
		for (int i = 0; i < mActualParticipation; i++) {
			players[i] = new Player();
		}
		//System.out.println("庄家编号(小于实际人数)");
		bankerNumber = random.nextInt(mActualParticipation);//第一次谁坐庄是随机的
		if (bankerNumber < 0 || bankerNumber > mActualParticipation - 1) {
			System.out.println("庄家编号小于实际人数");
			return;
		}
		
		int round = 0;//局数
		for (; round < mRound; round++) {//开始牛牛
			//每次开始前重置玩家数据
			for (int i = 0; i < mActualParticipation; i++) {
				players[i].reSet();
			}
			
			disorganize(50);//打乱牌
			for(int i = 0; i < mActualParticipation; i++) {//发牌
				Card[] mPlayerCards = new Card[NUMBER_OF_CARDS];
				for(int j = 0; j < NUMBER_OF_CARDS; j++) {
					mPlayerCards[j] = mCards[i * NUMBER_OF_CARDS + j];
				}
				players[i].setCards(mPlayerCards);
			}
			for(int i = 0; i < mActualParticipation; i++) {//获取牌信息
				players[i].getConcreteCards();
			}
			
			if (isFixed) {//固定庄
				players[bankerNumber].setBanker(true);//设置庄家
			} else {//破牛庄
				if (players[bankerNumber].ismLastRoundPoNiu()) {//如果庄家上一局破牛
					bankerNumber = (bankerNumber + 1) % mActualParticipation;//庄家序号后移
					players[bankerNumber].setBanker(true);//设置庄家
				} else {
					players[bankerNumber].setBanker(true);//设置庄家
				}
			}
			
			System.out.println("这一局的庄家是" + bankerNumber + "号");
			
			
			for (int i = 0; i < mActualParticipation; i++) {//设置闲家的赌注倍率
				if (players[i].isBanker()) {
					continue;
				}
				players[i].setmBetRatio(RATIO[random.nextInt(RATIO.length)]);
			}
			
			for (int i = 0; i < mActualParticipation; i++) {
				System.out.println(i + ": ");
				players[i].computeNiuNiu();
				if (players[i].isHasNiuNiu()) {
					players[i].setmLastRoundPoNiu(false);//有牛
					players[i].computeResidueSum();
				} else {
					players[i].setmLastRoundPoNiu(true);
					System.out.println("玩家" + i + "破牛了");
				}
			}
			
			for (int i = 0; i < mActualParticipation; i++) {//计算输赢
				if (players[i].isBanker())
					continue;
				if (players[i].isHasNiuNiu() && players[bankerNumber].isHasNiuNiu()) { //都有牛的情况
					if (players[i].isFiveTen() && !players[bankerNumber].isFiveTen()) {
						System.out.println("闲家 " + i + "赢5倍");
						players[i].setmWinRatio(WinBetRatio.FIVE_TIMES);
					} else if (!players[i].isFiveTen() && players[bankerNumber].isFiveTen()) {
						System.out.println("庄家赢5倍");
						players[i].setmWinRatio(WinBetRatio.MINUS_FIVE_TIMES);//庄家赢5倍就是闲家输5倍
					} else if (players[i].isFiveTen() && players[bankerNumber].isFiveTen()) {
						if (players[i].getMaxCardsNum() > players[bankerNumber].getMaxCardsNum()) {
							System.out.println("闲家 " + i + "赢5倍");
							players[i].setmWinRatio(WinBetRatio.FIVE_TIMES);
						} else if (players[i].getMaxCardsNum() < players[bankerNumber].getMaxCardsNum()) {
							System.out.println("庄家赢5倍");
							players[i].setmWinRatio(WinBetRatio.MINUS_FIVE_TIMES);
						} else {
							if (players[i].getMaxCardsColor() > players[bankerNumber].getMaxCardsColor()) {
								System.out.println("闲家 " + i + "赢5倍");
								players[i].setmWinRatio(WinBetRatio.FIVE_TIMES);
							}
							else {
								System.out.println("庄家赢5倍");
								players[i].setmWinRatio(WinBetRatio.MINUS_FIVE_TIMES);
							}
						}
					} else {
						if (players[i].getResidueSum() % 10 == 0 && players[bankerNumber].getResidueSum() % 10 != 0) {
							System.out.println("闲家 " + i + "赢3倍");
							players[i].setmWinRatio(WinBetRatio.THREE_TIMES);
						} else if (players[i].getResidueSum() % 10 != 0 && players[bankerNumber].getResidueSum() % 10 == 0) {
							System.out.println("庄家赢3倍");
							players[i].setmWinRatio(WinBetRatio.MINUS_THREE_TIMES);
						} else if (players[i].getResidueSum() % 10 == 0 && players[bankerNumber].getResidueSum() % 10 == 0) {
							if (players[i].getMaxCardsNum() > players[bankerNumber].getMaxCardsNum()) {
								System.out.println("闲家 " + i + "赢3倍");
								players[i].setmWinRatio(WinBetRatio.THREE_TIMES);
							} else if (players[i].getMaxCardsNum() < players[bankerNumber].getMaxCardsNum()) {
								System.out.println("庄家赢3倍");
								players[i].setmWinRatio(WinBetRatio.MINUS_THREE_TIMES);
							} else {
								if (players[i].getMaxCardsColor() > players[bankerNumber].getMaxCardsColor()) {
									System.out.println("闲家 " + i + "赢3倍");
									players[i].setmWinRatio(WinBetRatio.THREE_TIMES);
								} else {
									System.out.println("庄家赢3倍");
									players[i].setmWinRatio(WinBetRatio.MINUS_THREE_TIMES);
								}
							}
						} else {
							if (players[i].getResidueSum() % 10 > players[bankerNumber].getResidueSum() % 10) {
								if (players[i].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
									System.out.println("闲家 " + i + "赢2倍");
									players[i].setmWinRatio(WinBetRatio.TWO_TIMES);
								} else {
									System.out.println("闲家 " + i + "赢1倍");
									players[i].setmWinRatio(WinBetRatio.ONE_TIMES);
								}
							} else if (players[i].getResidueSum() % 10 < players[bankerNumber].getResidueSum() % 10) {
								if (players[bankerNumber].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
									System.out.println("庄家赢2倍");
									players[i].setmWinRatio(WinBetRatio.MINUS_TWO_TIMES);
								} else {
									System.out.println("庄家赢1倍");
									players[i].setmWinRatio(WinBetRatio.MINUS_ONE_TIMES);
								}
							} else {
								if (players[i].getMaxCardsNum() > players[bankerNumber].getMaxCardsNum() && players[i].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
									System.out.println("闲家 " + i + "赢2倍");
									players[i].setmWinRatio(WinBetRatio.TWO_TIMES);
								} else if (players[i].getMaxCardsNum() > players[bankerNumber].getMaxCardsNum() && players[i].getResidueSum() % 10 < mCriticalOfTwoTimes) {
									System.out.println("闲家 " + i + "赢1倍");
									players[i].setmWinRatio(WinBetRatio.ONE_TIMES);
								} else if (players[i].getMaxCardsNum() < players[bankerNumber].getMaxCardsNum() && players[bankerNumber].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
									System.out.println("庄家赢2倍");
									players[i].setmWinRatio(WinBetRatio.MINUS_TWO_TIMES);
								} else if (players[i].getMaxCardsNum() < players[bankerNumber].getMaxCardsNum() && players[bankerNumber].getResidueSum() % 10 < mCriticalOfTwoTimes) {
									System.out.println("庄家赢1倍");
									players[i].setmWinRatio(WinBetRatio.MINUS_ONE_TIMES);
								} else {
									if (players[i].getMaxCardsColor() > players[bankerNumber].getMaxCardsColor() && players[i].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
										System.out.println("闲家 " + i + "赢2倍");
										players[i].setmWinRatio(WinBetRatio.TWO_TIMES);
									} else if (players[i].getMaxCardsColor() > players[bankerNumber].getMaxCardsColor() && players[i].getResidueSum() % 10 < mCriticalOfTwoTimes) {
										System.out.println("闲家 " + i + "赢1倍");
										players[i].setmWinRatio(WinBetRatio.ONE_TIMES);
									} else if (players[i].getMaxCardsColor() < players[bankerNumber].getMaxCardsColor() && players[i].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
										System.out.println("庄家赢2倍");
										players[i].setmWinRatio(WinBetRatio.MINUS_TWO_TIMES);
									} else {
										System.out.println("庄家赢1倍");
										players[i].setmWinRatio(WinBetRatio.MINUS_ONE_TIMES);
									}
								}
							}
							
						}
					}
				} else if (players[i].isHasNiuNiu() && !players[bankerNumber].isHasNiuNiu()) { //闲家有牛的情况
					if (players[i].isFiveTen()) {
						System.out.println("闲家 " + i + "赢5倍");
						players[i].setmWinRatio(WinBetRatio.FIVE_TIMES);
					} else if (players[i].getResidueSum() % 10 == 0) {
						System.out.println("闲家 " + i + "赢3倍");
						players[i].setmWinRatio(WinBetRatio.THREE_TIMES);
					} else if (players[i].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
						System.out.println("闲家 " + i + "赢2倍");
						players[i].setmWinRatio(WinBetRatio.TWO_TIMES);
					} else {
						System.out.println("闲家 " + i + "赢1倍");
						players[i].setmWinRatio(WinBetRatio.ONE_TIMES);
					}
				} else if (!players[i].isHasNiuNiu() && players[bankerNumber].isHasNiuNiu()) {//庄家有牛
					if (players[bankerNumber].isFiveTen()) {
						System.out.println("庄家赢5倍");
						players[i].setmWinRatio(WinBetRatio.MINUS_FIVE_TIMES);
					} else if (players[bankerNumber].getResidueSum() % 10 == 0) {
						System.out.println("庄家赢3倍");
						players[i].setmWinRatio(WinBetRatio.MINUS_THREE_TIMES);
					} else if (players[bankerNumber].getResidueSum() % 10 >= mCriticalOfTwoTimes) {
						System.out.println("庄家赢2倍");
						players[i].setmWinRatio(WinBetRatio.MINUS_TWO_TIMES);
					} else {
						System.out.println("庄家赢1倍");
						players[i].setmWinRatio(WinBetRatio.MINUS_ONE_TIMES);
					}
				} else {//都没有牛
					if (players[i].getMaxCardsNum() > players[bankerNumber].getMaxCardsNum()) {
						System.out.println("闲家 " + i +"赢1倍");
						players[i].setmWinRatio(WinBetRatio.ONE_TIMES);
					} else if (players[i].getMaxCardsNum() < players[bankerNumber].getMaxCardsNum()) {
						System.out.println("庄家赢1倍");
						players[i].setmWinRatio(WinBetRatio.MINUS_ONE_TIMES);
					} else {
						if (players[i].getMaxCardsColor() > players[bankerNumber].getMaxCardsColor()) {
							System.out.println("闲家赢1倍");
							players[i].setmWinRatio(WinBetRatio.ONE_TIMES);
						} else if (players[i].getMaxCardsColor() < players[bankerNumber].getMaxCardsColor()) {
							System.out.println("庄家赢1倍");
							players[i].setmWinRatio(WinBetRatio.MINUS_ONE_TIMES);
						}
					}
				}
			}
			int mBankerJF = 0;//庄家的积分
			for (int i = 0; i < mActualParticipation; i++) {
				if (players[i].isBanker()) {
					for (int j = 0; j < mActualParticipation; j++) {
						mBankerJF += players[j].getmBetRatio() * players[j].getmWinRatio();
					}
					mBankerJF = -mBankerJF;
					players[i].setmCredits(players[i].getmCredits() + mBankerJF);
					System.out.println("庄家本局比赛积分 = " + mBankerJF);
				} else {
					players[i].setmCredits(players[i].getmCredits() + players[i].getmBetRatio() * players[i].getmWinRatio());
					System.out.println("闲家" + i + "本局比赛积分 = " + players[i].getmBetRatio() * players[i].getmWinRatio() 
							+ "     下注倍率： " + players[i].getmBetRatio() + "  赢得的倍率：" + players[i].getmWinRatio());
				}
			}
			
			System.out.println("第" + (round + 1) + "局结束");
			System.out.println("===============================");
			System.out.println("===============================");
		}
		
		/**
		 * 局后统计
		 */
		System.out.println("最后局数为：" + round);//现在还没有中途退出的操作
		for (int i = 0; i < mActualParticipation; i++) {
			System.out.println("玩家" + i + "的有牛次数：  " + players[i].getmCountOfYouNiu()
					+ "      牛牛次数：    " + players[i].getmCountOfNiuNiu()
					+ "      五花次数(现在的五花次数不包含牛牛次数)：   " + players[i].getmCountOfWuHua()
					+ "      最后积分：    " + players[i].getmCredits());
		}
		
		
	}
	
	/**
	 * 将牌打乱顺序
	 * @param num打乱的次数，num越大打乱的越好
	 */
	private static void disorganize(int num) {
		for(int i = 0; i < num; i++) {
			int a = Math.abs(random.nextInt(52));
			int b = Math.abs(random.nextInt(52));
			Card c = mCards[a];
			mCards[a] = mCards[b];
			mCards[b] = c;
		}
	}
	
	//内部类区块
	
	class WinBetRatio {
		public static final int ONE_TIMES = 1;
		public static final int TWO_TIMES = 2;
		public static final int THREE_TIMES = 3;
		public static final int FIVE_TIMES = 5;
		
		public static final int MINUS_ONE_TIMES = -1;
		public static final int MINUS_TWO_TIMES = -2;
		public static final int MINUS_THREE_TIMES = -3;
		public static final int MINUS_FIVE_TIMES = -5;
	}
}
